{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<center><img src=\"../logo.png\" alt=\"Header\" style=\"width: 800px;\"/></center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "@Copyright (C): 2010-2020, Shenzhen Yahboom Tech  \n",
    "@Author: Liusen  \n",
    "@Date: 2020-02-04 16:20:02  \n",
    "@LastEditors: Liusen  \n",
    "@LastEditTime: 2020-02-04 16:20:02   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import cv2\n",
    "\n",
    "red_lower = np.array([0,43,46])\n",
    "red_upper = np.array([10,255,255])\n",
    "blue_lower = np.array([100,43,46])\n",
    "blue_upper = np.array([124,255,255])\n",
    "cap = cv2.VideoCapture(0)\n",
    "\n",
    "cap.set(3,320)\n",
    "cap.set(4,240)\n",
    "def ChestRed():\n",
    "    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n",
    "    mask = cv2.inRange(hsv, red_lower, red_upper)\n",
    "    mask = cv2.erode(mask, None, iterations=2)\n",
    "    mask = cv2.GaussianBlur(mask, (3, 3), 0)\n",
    "    return mask\n",
    "def ChestBule():\n",
    "    ret, frame = cap.read()\n",
    "    frame = cv2.GaussianBlur(frame, (5, 5), 0)\n",
    "    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n",
    "    mask = cv2.inRange(hsv, blue_lower, blue_upper)\n",
    "    mask = cv2.GaussianBlur(mask, (3, 3), 0)\n",
    "    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
    "    if 25 < len(cnts) < 29:\n",
    "        print(\"Blue!\")\n",
    "while 1:\n",
    "    ret,frame = cap.read()\n",
    "    frame = cv2.GaussianBlur(frame,(5,5),0)\n",
    "    hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)\n",
    "    mask = ChestRed()\n",
    "    ChestBule()\n",
    "    res = cv2.bitwise_and(frame,frame,mask=mask)\n",
    "    cnts = cv2.findContours(mask.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2]\n",
    "    if 20<len(cnts)<30:\n",
    "        print(\"Red!\")\n",
    "\n",
    "    cv2.imshow(\"mask\",mask)\n",
    "    cv2.imshow(\"res\",res)\n",
    "    if cv2.waitKey(5) == ord('q'):\n",
    "        break\n",
    "cap.release()\n",
    "cv2.destroyAllWindows() \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
